<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="css/eui.css">
</head>
<body>
<div id="app">
    <el-page-header style="background-color: #0095d7;color: white;line-height: 60px"
            @back="goBack()" content="发布商品页面"></el-page-header>
    <!--分割线-->
    <el-divder></el-divder>
    <!--name代表上传文件时 文件的参数名
        limit="1" 设置只能选择一张图片
    -->
   <el-card>
     <el-form>
         <el-form-item label="商品标题" label-width="80px">
             <el-input v-model="product.title"></el-input>
         </el-form-item>
         <el-form-item label="价格" label-width="80px">
             <el-input v-model="product.price"></el-input>
         </el-form-item>
         <el-form-item label="原价" label-width="80px">
             <el-input v-model="product.oldPrice"></el-input>
         </el-form-item>
         <el-form-item label="销量" label-width="80px">
             <el-input v-model="product.saleCount"></el-input>
         </el-form-item>
         <el-form-item label="分类" label-width="80px">
             <template>
                 <el-select v-model="product.categoryId" placeholder="请选择">
                     <el-option v-for="c in categoryArr"
                                :label="c.name"
                                :value="c.id">
                     </el-option>
                 </el-select>
             </template>
         </el-form-item>
         <el-form-item label="商品图片" label-width="80px">
             <el-upload
                     action="/upload"
                     name="picFile"
                     :limit="1"
                     list-type="picture-card"
                     :on-preview="handlePictureCardPreview"
                     :on-success="handleSuccess"
                     :on-remove="handleRemove">
                 <i class="el-icon-plus"></i>
             </el-upload>
             <el-dialog :visible.sync="dialogVisible">
                 <img width="100%" :src="dialogImageUrl" alt="">
             </el-dialog>
         </el-form-item>
     </el-form>
       <el-button @click="insert()">发布</el-button>
   </el-card>
</div>
</body>
<!-- import Vue before Element -->
<script src="js/vue.js"></script>
<!-- import JavaScript -->
<script src="js/eui.js"></script>
<script src="js/axios.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                product:{
                    title:"",
                    url:"",
                    price:0,
                    oldPrice:0,
                    saleCount:0,
                    categoryId:""
                },
                categoryArr:[]
            }
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
                //当点击删除图片时方法会执行
                //file代表要删除的文件
                //file.response代表文件上传成功时 服务器响应的数据(文件名)
                console.log("文件名="+file.response);
                //http://localhost:8080/remove?name=xxx.jpg
                axios.get("/remove?name="+file.response).then(function (response) {
                    console.log("服务器图片已经删除")
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            insert(){
                //得到用户输入的微博文本内容和图片名 一起提交给服务器
                if (v.product.url==""){
                    alert("图片不能为空!")
                    return;
                }
                axios.post("/product/insert",v.product).then(function (response) {
                        alert("添加完成!");
                        //history.back();通过返回上一页面的方法 是不会发出请求的
                    //需要再次刷新才显示最新的内容
                        location.href="/admin.html"; //显示管理页面
                })
            },
            handleSuccess(response,file,fileList){
                //response=file.response
                console.log("文件上传完成, 图片名="+response);
                v.product.url = response;
            },
            goBack(){
                //返回上一页面
                history.back();
            }
        },
        created:function () {
            axios.get("/category/select").then(function (response) {
                v.categoryArr = response.data;
            })
        }
    })
</script>
</html>